package myky.com.doctorapp.ui

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ExpandableListView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.github.nukc.stateview.Constant
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_find_doctor_by_disease.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.DepartMentAdapter
import myky.com.doctorapp.adapter.FindDoctorDiseaseAdapter
import myky.com.doctorapp.entity.GroupItemEntity
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FindDoctorByDieseContract
import myky.com.doctorapp.entity.DepatmentWrapperEntity
import myky.com.doctorapp.presenter.FindDoctorByDiesePresenter
import myky.com.doctorapp.utils.ToastUtils

class FindDoctorByDiseaseActivity : BaseActivity<FindDoctorByDiesePresenter, MutableList<DepatmentWrapperEntity.Depatment.DepartmentEntity>>(), FindDoctorByDieseContract.View, ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener, BaseQuickAdapter.OnItemChildClickListener {
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        diseaseAdapter?.let {
            val data = it.data
            val diseaseName = data[position].diseaseName
            ToastUtils.show(diseaseName)
        }


    }

    private var lastSelectGroupPostion = 0
    private var lastSelectChildPostion = 0
    override fun onChildClick(parent: ExpandableListView?, p1: View?, groupPosition: Int, childPosition: Int, p4: Long): Boolean {

        diseaseItems.clear()

        depaetMentAdapter?.let {

            val childOld = it.getChild(lastSelectGroupPostion, lastSelectChildPostion)
            childOld.isHasSelected = false
            lastSelectChildPostion = childPosition
            lastSelectGroupPostion = groupPosition
            val child = it.getChild(groupPosition, childPosition)

            child.isHasSelected = true
            diseaseItems.addAll(child.diseaseItems)
            it.notifyDataSetChanged()


        }

        diseaseAdapter?.notifyDataSetChanged()


        return true

    }

    override fun onGroupClick(parent: ExpandableListView?, p1: View?, groupPosition: Int, p3: Long): Boolean {

        depaetMentAdapter?.let {
            val groupCount = it.groupCount
            for (i in 0 until groupCount) {
                if (i != groupPosition) {
                    expertDepartmentListView.collapseGroup(i)
                }
            }

            val groupOld = it.getGroup(lastSelectGroupPostion)
            groupOld.isHasSelected = false
            val group = it.getGroup(groupPosition)
            group.isHasSelected = true

            val items = group.items

            diseaseItems.clear()


            if (items.size > 0) {
                val child = it.getChild(groupPosition, 0)
                child.isHasSelected = true
                val temp = it.getChild(groupPosition, 0).diseaseItems
                diseaseItems.addAll(temp)
            }
            it.notifyDataSetChanged()

        }
        diseaseAdapter?.notifyDataSetChanged()
        expertDepartmentListView.setSelectedGroup(groupPosition)
        lastSelectGroupPostion = groupPosition


        val groupExpanded = parent?.isGroupExpanded(groupPosition)

        groupExpanded?.let {
            if (!it) {
                expertDepartmentListView.expandGroup(groupPosition, true)
            }
        }

        return true
    }

    var depaetMentAdapter: DepartMentAdapter? = null
    var diseaseAdapter: FindDoctorDiseaseAdapter? = null
    lateinit var departments: MutableList<GroupItemEntity>
    lateinit var diseaseItems: MutableList<GroupItemEntity.ChildItemeEntity.DiseaseItem>
    override fun showDeapartMent(department: MutableList<GroupItemEntity>) {
        department.let { departments.addAll(it) }

        depaetMentAdapter?.notifyDataSetChanged()

        expertDepartmentListView.expandGroup(0, true)

        val diseaseItems1 = departments[0].items[0].diseaseItems
        diseaseItems.addAll(diseaseItems1)
        diseaseAdapter?.notifyDataSetChanged()
    }

    override fun showDataSuccess(datas: MutableList<DepatmentWrapperEntity.Depatment.DepartmentEntity>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {
        mPresenter.findDepartMent()
        departments = ArrayList()
        diseaseItems = ArrayList()

        depaetMentAdapter = DepartMentAdapter(this, departments)
        diseaseAdapter = FindDoctorDiseaseAdapter(diseaseItems)
        diseaseListView.layoutManager = LinearLayoutManager(this)
        diseaseListView.adapter = diseaseAdapter
        expertDepartmentListView.setAdapter(depaetMentAdapter)

        expertDepartmentListView.setOnGroupClickListener(this)
        expertDepartmentListView.setOnChildClickListener(this)
        diseaseAdapter?.onItemChildClickListener = this


    }

    override fun initView() {

        val extras = intent.extras

        conmonTitleTextView.text = extras.getString(Constant.FINDDOCTORTITLE)

        searchTextView.text = extras.getString(Constant.FINDDOCTORSEARCHHINT)

        backLayout.setOnClickListener { finish() }

        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.commonTitleLayout)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init()
    }

    override fun getContenView(): Int = R.layout.activity_find_doctor_by_disease

    override fun reloadActivity() {
    }

    override fun getPresenter() = FindDoctorByDiesePresenter()
}
